home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / allocwg.com / DUMPHEAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-10  |  5.7 KB  |  172 lines

  1. #ifndef MSC
  2.  
  3.     /*  Non-MSC Version  */
  4.  
  5.  
  6.     /*  NOTE:  This  procedure  should only be used for
  7.                large data model programs (i.e., Compact, Large, Huge) */
  8.  
  9. #if (defined(M_I86CM) || defined(M_I86LM) || defined(M_I86HM))
  10.  
  11. #include <stdio.h>
  12. #include "alloc.h"
  13.  
  14. extern ATABLE  AllocationTable [] ; /* Provide storage for block pointers */
  15. extern SUNIT   MBSize             ; /* Default allocation block size      */
  16. extern SUNIT   NEntry             ; /* Number of table entries            */
  17. extern size_t  _asizds            ;
  18. extern size_t  _abrktb []         ;
  19. extern size_t  _psp               ;
  20.  
  21. DumpHeap ( msg )
  22.  
  23.    char msg []  ;    /*  Message to be displayed at beginning of dump  */
  24.  
  25. /* 
  26.    +----------------------------------------------------+
  27.    |                                                    |
  28.    |   This procedure is called to dump to a file       |
  29.    |   called 'memory.dmp' the current memory block     |
  30.    |   sizes.  (Replacement Version)                    |
  31.    |                                                    |
  32.    +----------------------------------------------------+
  33. */
  34.  
  35. {
  36.    FILE        *file         ;
  37.    FILE        *fopen ()     ;
  38.    HEADER      *Header       ;
  39.    long codesize, dataused, dataalloced, allocused, allocated ;
  40.    long  totalf, totalu      ;
  41.    SUNIT offset, *bptr, bsize;
  42.    int   i                   ;
  43.    int   totalinuse          ;
  44.    int   totalfree           ;
  45.    char string [ 83]         ;
  46.    char buffer [BUFSIZ]      ;
  47.  
  48.    if ((file = fopen ( "memory.new", "a" )) == NULL )
  49.       return ;
  50.    setbuf ( file, buffer ) ;
  51.  
  52.    fprintf ( file, "\n\n **** Heap Dump **** \n" ) ;
  53.    fprintf ( file, "Message: %s\n", msg ) ;
  54.    ProgSize (&codesize, &dataused, &dataalloced, &allocused, &allocated) ;
  55.    fprintf ( file, 
  56.       "C: %6ld   DU: %6ld  DA: %6ld     AU: %6ld  AA: %6ld     Total: %6ld\n",
  57.           codesize,  dataused, dataalloced, allocused, allocated, 
  58.           codesize + dataalloced + allocated ) ;
  59.  
  60.    totalf = totalu = 0L ;
  61.    totalinuse = totalfree = 0 ;
  62.    for ( i = 0 ; i < NEntry ; i++ ) {
  63.       Header = AllocationTable [i].Header ;
  64.       fprintf ( file, 
  65.         "\nBlock Pointer: %p, Block Size: %u, Collapsed: %d, LFBlock: %u\n",
  66.         Header, AllocationTable [i].Size, Header->Collapsed, Header->LFBlock ) ;
  67.  
  68.       offset  = HSIZE ;
  69.       while ( offset < Header->BytesUsed ) {
  70.          bptr  = (SUNIT *) Header + offset/SSIZE ;
  71.          bsize = *bptr & ~FREE ;
  72.          fprintf ( file, "Heap Pointer - %p, Size %5u", bptr+1, bsize ) ;
  73.          if ( (*bptr & FREE) ) {
  74.             fprintf ( file, ", FREE\n" ) ;
  75.             totalf += (long) bsize ;
  76.             totalfree++ ;
  77.          }
  78.          else {
  79.             fprintf ( file, ", **IN USE**\n" ) ;
  80.             totalu += (long) bsize ;
  81.             totalinuse++ ;
  82.          } ;
  83.          offset += bsize+SSIZE  ;
  84.       } ;
  85.       if ( offset != Header->BytesUsed )
  86.          fprintf ( file, "*** Size Error, Header->BytesUsed - %u, offset - %u\n",
  87.                          Header->BytesUsed, offset ) ;
  88.    } ;
  89.    fprintf ( file, "\nTotal space in Use  - %7ld"  , totalu ) ;
  90.    fprintf ( file, "\nTotal space free    - %7ld"  , totalf ) ;
  91.    fprintf ( file, "\nTotal number in Use - %7d"   , totalinuse ) ;
  92.    fprintf ( file, "\nTotal number free   - %7d\n" , totalfree  ) ;
  93.    fclose ( file ) ;
  94. }
  95. #endif
  96.  
  97. #else
  98.  
  99.    /*  MSC Version  */
  100.  
  101. #include <stdio.h>
  102. #include <dos.h>
  103. #include <malloc.h>
  104.  
  105. DumpHeap (msg)
  106.  
  107.    char msg []  ;
  108.  
  109. /* 
  110.    +----------------------------------------------------+
  111.    |                                                    |
  112.    |   This procedure is called to dump to a file       |
  113.    |   called 'memory.dmp' the current memory block     |
  114.    |   sizes.   (MSC Version)                           |
  115.    |                                                    |
  116.    +----------------------------------------------------+
  117. */
  118.  
  119. {
  120.    FILE *file               ;
  121.    FILE *fopen ()           ;
  122.    _HEAPINFO   hinfo        ;
  123.    long codesize, dataused, dataalloced, allocused, allocated ;
  124.    long  totalf, totalu     ;
  125.    char string [ 83]        ;
  126.    char buffer [BUFSIZ]     ;
  127.    int   totalinuse         ;
  128.    int   totalfree          ;
  129.    size_t lastseg           ;
  130.  
  131.  
  132.    if ((file = fopen ( "memory.msc", "a" )) == NULL )
  133.      return ;
  134.    setbuf ( file, buffer ) ;
  135.  
  136.    fprintf ( file, "\n\n **** Heap Dump **** \n" ) ;
  137.    fprintf ( file, "Message: %s\n", msg ) ;
  138.    ProgSize (&codesize, &dataused, &dataalloced, &allocused, &allocated) ;
  139.    fprintf ( file, 
  140.       "C: %6ld   DU: %6ld  DA: %6ld     AU: %6ld  AA: %6ld     Total: %6ld\n",
  141.           codesize,  dataused, dataalloced, allocused, allocated, 
  142.           codesize + dataalloced + allocated ) ;
  143.  
  144.    hinfo._pentry = NULL ;
  145.    totalf = totalu = 0L ;
  146.    lastseg= 0 ;
  147.    totalinuse = totalfree = 0 ;
  148.    while ( _heapwalk ( &hinfo ) == _HEAPOK ) {
  149.       if ( lastseg != FP_SEG (hinfo._pentry) )
  150.          fprintf ( file, "\n" ) ;
  151.       lastseg = FP_SEG (hinfo._pentry) ;
  152.       fprintf ( file, "Heap Pointer - %p, Size %5u", hinfo._pentry,
  153.                        hinfo._size ) ;
  154.       if ( hinfo._useflag == _USEDENTRY ) {
  155.          fprintf ( file, ", **IN USE**\n" ) ;
  156.          totalu += (long) hinfo._size ;
  157.          totalinuse++ ;
  158.       }
  159.       else {
  160.          fprintf ( file, ", FREE\n" ) ;
  161.          totalf += (long) hinfo._size ;
  162.          totalfree++ ;
  163.       } ;
  164.    } ;
  165.    fprintf ( file, "\nTotal in Use        - %7ld"  , totalu ) ;
  166.    fprintf ( file, "\nTotal free          - %7ld"  , totalf ) ;
  167.    fprintf ( file, "\nTotal number in Use - %7d"   , totalinuse ) ;
  168.    fprintf ( file, "\nTotal number free   - %7d\n" , totalfree  ) ;
  169.    fclose  ( file ) ;
  170. }
  171. #endif
  172.